<?php

namespace app\teacher\model;

use app\common\model\BaseModel;
use think\Model;

class User extends BaseModel
{
    public function course()
    {
        return $this->belongsTo(Course::class, 'user_id', 'user_id')->where('status', 4)->order('updatetime desc');
    }
    /**
     * 获取用户信息
     */
    public static function getByField($whereField, $field)
    {
        return self::where($whereField, $field)->find();
    }

    /**
     * 教师授课学生
     */
    public static function getStudentByTeacher($teacherId)
    {
        $studentId = Course::getStudents($teacherId);
        $students = self::with('course')->field('user_id, nickname, avatar')->whereIn('user_id', $studentId)->select();
        array_walk($students, function (&$val) {
            $val['time'] = $val['course'] ? $val['course']['updatetime'] : '';
            unset($val['course']);
        });
        return $students;
    }

    public static function getUserInfo($userId)
    {
        return self::field('user_id, nickname, avatar, mobile')->find($userId);
    }
}
